package com.glorysoft.xztpda.ui.changeshift

import android.content.Intent
import android.os.Bundle
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityChangeShiftBinding
import com.glorysoft.xztpda.entity.ChangeShiftEntity
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import kotlinx.android.synthetic.main.activity_batch_track_in.*
import kotlinx.android.synthetic.main.activity_batch_track_in.etEqpID
import kotlinx.android.synthetic.main.activity_change_shift.*
import kotlinx.android.synthetic.main.activity_change_shift.etLotID
import kotlinx.android.synthetic.main.activity_change_shift.ivLotScan
import kotlinx.android.synthetic.main.activity_track_in_guide.*

/**
 * FileName: ChangeShiftActivity
 * Author: XJH
 * Date: 2022/4/13 17:21
 * Description:
 * History:
 */
class ChangeShiftActivity : BaseActivity<ChangeShiftViewModel, ActivityChangeShiftBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_change_shift
    }

    override fun initVariableId(): Int {
        return BR.changeShiftViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("交班")
        setRightListener("提交") {

            if (viewModel.shiftItems.size <= 0) {
                viewModel.defUI.toastEvent.value = ToastMessageBean("请添加列表信息后操作", false)
                return@setRightListener
            }

            for (entity in viewModel.shiftItems) {
                if (entity.actioN_QTY.equals("")) {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("已完成数量不能为空", false)
                    return@setRightListener
                }

                try {
                    if (!viewModel.stepList.contains(entity.steP_NAME)){
                        if (entity.actioN_QTY.toInt() > entity.maiN_QTY.toInt()){
                            viewModel.defUI.toastEvent.value = ToastMessageBean("已完成数量不能大于批次数量", false)
                            return@setRightListener
                        }
                    }
                }catch (e : Exception){}

            }

            viewModel.shift()
        }

        ivLotScan.setOnClickListener{startScanForCode(this,10001)}
    }

    override fun initData() {
        initEdit(etLotID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getLotInfo(s)
                etLotID.setText("")
            }

        })

        viewModel.defUI.callObserve.observe(this){
            viewModel.defUI.toastEvent.value = ToastMessageBean("交班成功", true)
            finish()
        }

        viewModel.defUI.startActivity.observe(this){
            val currentFocus = currentFocus
            currentFocus?.clearFocus()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK){
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when(requestCode){
                        10001 -> {
                            viewModel.getLotInfo(barCode)
                        }
                    }
                }
            }


        }

    }
}